# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import sys
import caffe

def Gender_Recognition(img_path):
    plt.rcParams['figure.figsize'] = (10,10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'
    caffe_root = 'C:/Program Files/caffe/caffe-master/caffe-master'
    sys.path.insert(0, caffe_root+'python')
    caffe.set_mode_cpu()
    config_root='F:/face-recognition/age_gender/'
    model_def = config_root+'deploy_gender.prototxt'
    model_weights = config_root+'gender_net.caffemodel'
    mean_filename = config_root+'mean.npy'
    net = caffe.Net(model_def, model_weights, caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2,0,1))
    transformer.set_mean('data',np.load(mean_filename).mean(1).mean(1))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))
    net.blobs['data'].reshape(1,3,227,227)
    image = caffe.io.load_image(img_path)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    labels = ['male','female']
    output = net.forward()
    output_prob = output['prob'][0]
    # print output_prob
    return labels[output_prob.argmax()]

def Age_Recognition(img_path):
    plt.rcParams['figure.figsize'] = (10,10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'
    caffe_root = 'C:/Program Files/caffe/caffe-master/caffe-master'
    sys.path.insert(0, caffe_root+'python')
    caffe.set_mode_cpu()
    config_root='F:/face-recognition/age_gender/'
    model_def = config_root+'deploy_age.prototxt'
    model_weights = config_root+'age_net.caffemodel'
    mean_filename = config_root+'mean.npy'
    net = caffe.Net(model_def, model_weights, caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2,0,1))  
    # transformer.set_mean('data', mean)
    transformer.set_mean('data',np.load(mean_filename).mean(1).mean(1))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))
    net.blobs['data'].reshape(1,3,227,227)
    image = caffe.io.load_image(img_path)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    labels = ['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
    output = net.forward()
    output_prob = output['prob'][0]
    # print output_prob
    return labels[output_prob.argmax()]


#How to use:
# test_path='F:/face-recognition/age_gender/'+'zj.jpg'
# r=Gender_Recognition(test_path)
# print 'The photo named ',test_path,'map the gender of ',r
# r=Age_Recognition(test_path)
# print 'The photo named ',test_path,'map the Age of ',r


def pre_face(cf,md,mw,mm):
    plt.rcParams['figure.figsize'] = (10,10)
    plt.rcParams['image.interpolation'] = 'nearest'
    plt.rcParams['image.cmap'] = 'gray'
    caffe_root=cf
    sys.path.insert(0, caffe_root+'python')
    caffe.set_mode_cpu()
    model_def=md
    model_weights=mw
    mean_filename=mm
    net = caffe.Net(model_def, model_weights, caffe.TEST)
    transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
    transformer.set_transpose('data', (2,0,1))
    transformer.set_mean('data',np.load(mean_filename).mean(1).mean(1))
    transformer.set_raw_scale('data', 255)
    transformer.set_channel_swap('data', (2,1,0))
    net.blobs['data'].reshape(1,3,227,227)
    return (transformer,net)

def Gender_batch(tf,nt,img_path):
    transformer=tf
    net=nt
    image = caffe.io.load_image(img_path)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    labels = ['男','女']
    output = net.forward()
    output_prob = output['prob'][0]
    return labels[output_prob.argmax()]

def Age_batch(tf,nt,img_path):
    transformer=tf
    net=nt
    image = caffe.io.load_image(img_path)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    labels = ['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
    output = net.forward()
    output_prob = output['prob'][0]
    return labels[output_prob.argmax()]

# config_root='F:/face-recognition/age_gender/'
# caffe_root = 'C:/Program Files/caffe/caffe-master/caffe-master'
# model_def = config_root+'deploy_gender.prototxt'
# model_weights = config_root+'gender_net.caffemodel'
# mean_filename = config_root+'mean.npy'
# img_path=config_root+'lzh.jpg'
# tf,nt=pre_face(caffe_root,model_def,model_weights,mean_filename)
# r=Gender_batch(tf,nt,img_path)
# print r
# r=Age_batch(tf,nt,img_path)
# print r